package com.xiaoKe.zhonghzu;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xiaoKe.zhonghzu.databinding.ItemMainBinding;
import com.xiaoKe.zhonghzu.global.bean.MainBean;
import com.xiaoKe.zhonghzu.global.tool.ResourceTool;
import com.xiaoKe.zhonghzu.ui.onClick.OnClickListenerPositionInt;

import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/8/10 14:30
 * @Description : 文件描述
 */
public class MainActivityAdapter  extends RecyclerView.Adapter<MainActivityAdapter.ViewHolder> {
    List<MainBean> list;
    private int isPosition = 0;
    public MainActivityAdapter(List<MainBean> list) {
        this.list= list;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        ItemMainBinding binding = ItemMainBinding.inflate(inflater, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        holder.binding.mainImg.setImageResource(position ==isPosition? list.get(position).getSelectedImg():list.get(position).getNoSelectedImg());
        holder.binding.mainTv.setTextColor(position ==isPosition? ResourceTool.resColor(R.color.colorMain) :ResourceTool.resColor(R.color.color333));
        holder.binding.mainTv.setText(list.get(position).getName());
        holder.binding.linerAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isPosition = position;
                notifyDataSetChanged();
                onClickListenerPositionInt.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        final ItemMainBinding binding;
        ViewHolder(ItemMainBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

    public OnClickListenerPositionInt onClickListenerPositionInt ;

    public void setOnClickListenerPositionInt(OnClickListenerPositionInt onClickListenerPositionInt) {
        this.onClickListenerPositionInt = onClickListenerPositionInt;
    }
}
